package com.fare.fdc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.xalan.lib.sql.QueryParameter;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.report.csair.CsairReportApplication;
import com.fare.taluo.auth.security.ContextLoginUser;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.report.CsairReportVO;

@Scope("prototype")
@Controller("lineSpreadMonitorAction")
public class LineSpreadMonitorAction extends BaseFdcAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7352773899288042535L;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;
	@Resource(name = "csairReportApplication")
	private CsairReportApplication csairReportApplication;
	private   List<SourceEntityVO>  sourceLists;
	private String carrier;
	private String fromCity;
	private String toCity;
	private String fromCityCode;
	private String toCityCode;
	private String flightStartDate;
	private String flightEndDate;
	private String channel;
	private String language;
	private static final String WEB_TYPE = "1";
	public void queryData() throws JsonGenerationException, JsonMappingException, IOException{
		List<CsairReportVO> voList = csairReportApplication.findCsairEntityByCondition(carrier, fromCityCode, toCityCode, flightStartDate, flightEndDate,null, DateUtils.formatDay(new Date(), "yyyy-MM-dd"), channel, WEB_TYPE);
		Locale locale = super.getLocale();
		//航线相同航班放一起
		//Map<String,List<CsairReportVO>> lineMap = getSameLineMap(voList);
		I18nUtil.queryDataI18N(voList, locale);
		Map<String,List<CsairReportVO>> lineMap = getSameLineMap(voList);
		String jsonStr = JsonUtil.toJson(lineMap);
		String data = "{\"jsonSize\":"+lineMap.size()+","+"\"jsonString\":"+jsonStr+"}";
		super.toJSONOut(data);
	}
	
	public String toAnalyseJSP() throws Exception {
		Locale locale = super.getLocale();
		sourceLists=sourceApplication.findCrawlAirCompanys();
		I18nUtil.sourceConvert2I18N(sourceLists, locale);
		this.setCarrier(ContextLoginUser.getLoginUser().getSourceKey());
		returnPageURL = "/fare/_lineSpreadMonitor.jsp";
		request.setAttribute("maxDate", DateUtils.getDateAfter(29));//目前只支持30天的数据 
		language=I18nUtil.showLanguage(locale);
		return "dispatcher";
	}
	
	/**
	 *  航线且日期相同的航班放一起
	 * @param voList
	 * @return
	 */
	public Map<String,List<CsairReportVO>> getSameLineMap(List<CsairReportVO> voList){
		Map<String,List<CsairReportVO>> map = new TreeMap<String, List<CsairReportVO>>();
		for (CsairReportVO csairReportVO : voList) {
			String lineAndDate = csairReportVO.getFromCity()+csairReportVO.getToCity()+csairReportVO.getFlightDateStr();
			List<CsairReportVO> list = null;
			if(map.containsKey(lineAndDate)){
				 list = map.get(lineAndDate);
				 list.add(csairReportVO);
			}else{
				 list = new ArrayList<CsairReportVO>();
				 list.add(csairReportVO);
			}
				 map.put(lineAndDate, list);
		}
		return map;
	}

	public String getCarrier() {
		return carrier;
	}

	public void setCarrier(String carrier) {
		this.carrier = carrier;
	}

	public List<SourceEntityVO> getSourceLists() {
		return sourceLists;
	}

	public void setSourceLists(List<SourceEntityVO> sourceLists) {
		this.sourceLists = sourceLists;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getFromCityCode() {
		return fromCityCode;
	}

	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}

	public String getFlightStartDate() {
		return flightStartDate;
	}

	public void setFlightStartDate(String flightStartDate) {
		this.flightStartDate = flightStartDate;
	}

	public String getFlightEndDate() {
		return flightEndDate;
	}

	public void setFlightEndDate(String flightEndDate) {
		this.flightEndDate = flightEndDate;
	}

	public String getChannel() {
		return channel;
	}

	public void setChannel(String channel) {
		this.channel = channel;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
	
	
}
